﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using coffeeshop.Models;

namespace coffeeshop.Forms
{
    public partial class frmOrder : Form
    {
        private const string COLUMN_NAME_CODE = "Mã";
        private const string COLUMN_NAME_TEN = "Tên";
        private const string COLUMN_NAME_SL = "SL";

        private bool quantityValidated = true;

        private coffeeDataSet.ProductDataTable productList { get; set; }

        public frmOrder()
        {
            InitializeComponent();
            LoadProducts();
            BindDataGrid();
        }

        private void LoadProducts()
        {
            productList = Product.GetProductList(true);
        }

        private void BindDataGrid()
        {
            DataGridViewComboBoxColumn cmbCode = new DataGridViewComboBoxColumn();
            cmbCode.HeaderText = COLUMN_NAME_CODE;
            cmbCode.Name = "cmbCode";
            cmbCode.DataSource = productList;
            cmbCode.DisplayMember = "Code";
            cmbCode.ValueMember = "ProductId";

            cmbCode.AutoComplete = true;
            cmbCode.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView1.Columns.Add(cmbCode);

            DataGridViewTextBoxColumn txtQuantity = new DataGridViewTextBoxColumn();
            txtQuantity.HeaderText = COLUMN_NAME_SL;
            txtQuantity.Name = "txtQuantity";
            dataGridView1.Columns.Add(txtQuantity);

            DataGridViewTextBoxColumn txtName = new DataGridViewTextBoxColumn();
            txtName.HeaderText = COLUMN_NAME_TEN;
            txtName.Name = "txtName";
            dataGridView1.Columns.Add(txtName);
            
            dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView1.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

            dataGridView1.Columns[0].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridView1.Columns[1].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridView1.Columns[2].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;

            dataGridView1.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
            dataGridView1.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridView1.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            dataGridView1.Columns[2].ReadOnly = true;
            dataGridView1.Columns[2].DefaultCellStyle.ForeColor = Color.BlueViolet;

            //dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[0];
            //dataGridView1.BeginEdit(true);
        }

        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (dataGridView1.CurrentCell.ColumnIndex == 0 && e.Control is ComboBox)
            {
                ComboBox comboBox = e.Control as ComboBox;
                //comboBox.AutoCompleteSource = productList;
                comboBox.AutoCompleteMode = AutoCompleteMode.Append;
                comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
                comboBox.SelectedIndexChanged += LastColumnComboSelectionChanged;
                comboBox.KeyPress += new KeyPressEventHandler(SelectProductDone);
            }

            if (dataGridView1.CurrentCell.ColumnIndex == 1 && e.Control is TextBox)
            {
                TextBox textBox = e.Control as TextBox;
                textBox.KeyPress += new KeyPressEventHandler(txtBox_KeyPress);
                textBox.LostFocus += new EventHandler(textBox_LostFocus);
            }
        }

        void txtBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            quantityValidated = false;
        }

        void textBox_LostFocus(object sender, EventArgs e)
        {
            if (quantityValidated)
                return;

            bool isPassed = true;
            var currentcell = dataGridView1.CurrentCellAddress;
            var sendingTextBox = sender as DataGridViewTextBoxEditingControl;

            DataGridViewTextBoxCell quantityCell = (DataGridViewTextBoxCell)dataGridView1.Rows[currentcell.Y].Cells[1];
                
            if (string.IsNullOrEmpty(sendingTextBox.Text.Trim()))
            {
                //MessageBox.Show("Xin nhập số lượng cho thành phần này");
                isPassed = false;
            }
            int quantity;
            if (!int.TryParse(sendingTextBox.Text.Trim(), out quantity) || quantity <= 0)
            {
                //MessageBox.Show("Xin nhập số lượng hợp lệ. Số nguyên và lớn hơn 0");
                isPassed = false;
            }

            quantityValidated = true;
            if (!isPassed)
            {
                // deu hieu sao ko hoat dong
                sendingTextBox.ForeColor = Color.Red;
                dataGridView1.CurrentCell = quantityCell;
                dataGridView1.CurrentCell.Selected = true;
                dataGridView1.BeginEdit(true);
            }
            else
            {
                sendingTextBox.ForeColor = Color.Black;
            }
        }

        private void LastColumnComboSelectionChanged(object sender, EventArgs e)
        {
            var currentcell = dataGridView1.CurrentCellAddress;
            var sendingCB = sender as DataGridViewComboBoxEditingControl;
            DataGridViewTextBoxCell nameCell = (DataGridViewTextBoxCell)dataGridView1.Rows[currentcell.Y].Cells[2];
            int productId;
            int.TryParse(sendingCB.SelectedValue == null ? string.Empty : sendingCB.SelectedValue.ToString(), out productId);
            nameCell.Value = GetProductNameByProductId(productId);

            // TODO: move to edit the price
            //DataGridViewTextBoxCell priceCell = (DataGridViewTextBoxCell)dataGridView1.Rows[currentcell.Y].Cells[1];
            //dataGridView1.CurrentCell = priceCell;
            //dataGridView1.BeginEdit(true);
        }

        private string GetProductNameByProductId(int productId)
        {
            if (productId == 0) return string.Empty;

            return productList.Where(u => u.ProductId == productId).Select(t => t.Name).First().ToString();
        }


        public void SelectProductDone(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Tab)
            {
                var currentcell = dataGridView1.CurrentCellAddress;
                var sendingCB = sender as DataGridViewComboBoxEditingControl;
                DataGridViewTextBoxCell quantityCell = (DataGridViewTextBoxCell)dataGridView1.Rows[currentcell.Y].Cells[2];
                dataGridView1.CurrentCell = quantityCell;
                dataGridView1.BeginEdit(true);
            }
        }
    }
}
